import {
  selectVehicleList,
  enableOrDisable,
  vehicleDetail,
  selectInfoDetail,
  infoOperate,
  vehicleOperate,
} from '@/services/fleetManage/vehicle';
import prompt from '@/components/Prompt';

export default {
  namespace: 'vehicle',

  state: {
    vehicleList: {}, // 车辆列表
    vehicleDetail: {}, // 车辆详情
    papersList: {},
    formValues: {},
  },

  effects: {
    /**
     * 查询车辆列表
     */
    *selectVehicleList({ payload, callback }, { call, put }) {
      const response = yield call(selectVehicleList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      yield put({
        type: 'saveVehicleList',
        payload: {
          vehicleList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },

    /**
     * 启用或禁用车辆
     */
    *enableOrDisable({ payload, callback }, { call, put }) {
      const response = yield call(enableOrDisable, payload);
      const { code, message } = response;
      const content = !payload.type ? `启用${message}` : `禁用${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },
    /**
     * 新增编辑车辆
     */
    *vehicleOperate({ payload, callback }, { call, put }) {
      const response = yield call(vehicleOperate, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      if (callback) callback(data);
    },

    /**
     * 车辆详情
     */
    *vehicleDetail({ payload, callback }, { call, put }) {
      const response = yield call(vehicleDetail, payload);
      const { code, data } = response;
      if (code !== 0) return;
      yield put({
        type: 'saveVehicleDetail',
        payload: {
          vehicleDetail: { [payload.id]: data },
          papersList: {
            [payload.id]: {
              list: data.papers,
            },
          },
        },
      });
      if (callback) callback(data);
    },

    /**
     * 证件信息详情
     */
    *selectInfoDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectInfoDetail, payload);
      const { code, data } = response;
      if (code !== 0) return;
      if (callback) callback(data);
    },

    /**
     * 新增编辑 证件
     */
    *infoOperate({ payload, callback }, { call, put }) {
      const response = yield call(infoOperate, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      if (callback) callback(data);
    },

    /**
     * 设置表单参数
     */
    *allValus({ payload }, { call, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    // 车辆列表
    saveVehicleList(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 车辆详情
    saveVehicleDetail(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
